import request from '@/utils/request'
import { GET_ARTICLE_LIST, GET_CHANNEL_LIST } from '../constants'

//获取频道列表
export const getChannelList = () => {
  return async (dispatch) => {
    const res = await request.get('/channels')
    // console.log(res)
    const channels = res.data.data.channels
    dispatch({
      type: GET_CHANNEL_LIST,
      payload: channels,
    })
  }
}

//获取文章列表
export const getArticleList = (params) => {
  return async (dispatch) => {
    const res = await request.get('/mp/articles', {
      params: params,
    })
    // console.log(res, 2222)
    dispatch({
      type: GET_ARTICLE_LIST,
      payload: res.data.data,
    })
  }
}

//删除文章
export const delArticle = (id) => {
  return async (dispatch) => {
    const res = await request.delete(`/mp/articles/${id}`)
    console.log(res)
  }
}

//新增文章
export const addArticle = (draft, data) => {
  return async (dispatch) => {
    const res = await request.post(`/mp/articles?draft=${draft}`, data)
    console.log(res)
  }
}

//获取文章详情
export const getArticleDetail = (id) => {
  return async (dispatch) => {
    const res = await request.get(`/mp/articles/${id}`)
    console.log(res)
    return res.data.data
  }
}

//编辑文章
// export const editArticle = (draft, data) => {
//   return async (dispatch) => {
//     await request({
//       method: 'put',
//       url: `/mp/article/${data.id}`,
//       params: {
//         draft,
//       },
//       data,
//     })
//   }
// }
export const editArticle = (draft, data) => {
  return async (dispatch) => {
    const res = await request.put(
      `/mp/articles/${data.id}?draft=${draft}`,
      data
    )
    console.log(res)
  }
}
